<?php
/*
	This SQL query will create the table to store your object.

	CREATE TABLE `remuneration` (
	`remunerationid` int(11) NOT NULL auto_increment,
	`type` VARCHAR(255) NOT NULL,
	`amount` FLOAT NOT NULL, PRIMARY KEY  (`remunerationid`)) ENGINE=MyISAM;
*/

/**
* <b>Remuneration</b> class with integrated CRUD methods.
* @author Php Object Generator
* @version POG 3.0f / PHP5.1 MYSQL
* @see http://www.phpobjectgenerator.com/plog/tutorials/45/pdo-mysql
* @copyright Free for personal & commercial use. (Offered under the BSD license)
* @link http://www.phpobjectgenerator.com/?language=php5.1&wrapper=pdo&pdoDriver=mysql&objectName=Remuneration&attributeList=array+%28%0A++0+%3D%3E+%27type%27%2C%0A++1+%3D%3E+%27amount%27%2C%0A++2+%3D%3E+%27GotStudentActivity%27%2C%0A%29&typeList=array%2B%2528%250A%2B%2B0%2B%253D%253E%2B%2527VARCHAR%2528255%2529%2527%252C%250A%2B%2B1%2B%253D%253E%2B%2527FLOAT%2527%252C%250A%2B%2B2%2B%253D%253E%2B%2527HASMANY%2527%252C%250A%2529
*/
include_once('class.pog_base.php');
class Remuneration extends POG_Base
{
	public $remunerationId = '';

	/**
	 * @var VARCHAR(255)
	 */
	public $type;
	
	/**
	 * @var FLOAT
	 */
	public $amount;
	
	/**
	 * @var private array of GotStudentActivity objects
	 */
	private $_gotstudentactivityList = array();
	
	public $pog_attribute_type = array(
		"remunerationId" => array('db_attributes' => array("NUMERIC", "INT")),
		"type" => array('db_attributes' => array("TEXT", "VARCHAR", "255")),
		"amount" => array('db_attributes' => array("NUMERIC", "FLOAT")),
		"GotStudentActivity" => array('db_attributes' => array("OBJECT", "HASMANY")),
		);
	public $pog_query;
	
	
	/**
	* Getter for some private attributes
	* @return mixed $attribute
	*/
	public function __get($attribute)
	{
		if (isset($this->{"_".$attribute}))
		{
			return $this->{"_".$attribute};
		}
		else
		{
			return false;
		}
	}
	
	function Remuneration($type='', $amount='')
	{
		$this->type = $type;
		$this->amount = $amount;
		$this->_gotstudentactivityList = array();
	}
	
	
	/**
	* Gets object from database
	* @param integer $remunerationId 
	* @return object $Remuneration
	*/
	function Get($remunerationId)
	{
		$connection = Database::Connect();
		$this->pog_query = "select * from `remuneration` where `remunerationid`='".intval($remunerationId)."' LIMIT 1";
		$cursor = Database::Reader($this->pog_query, $connection);
		while ($row = Database::Read($cursor))
		{
			$this->remunerationId = $row['remunerationid'];
			$this->type = $this->Unescape($row['type']);
			$this->amount = $this->Unescape($row['amount']);
		}
		return $this;
	}
	
	
	/**
	* Returns a sorted array of objects that match given conditions
	* @param multidimensional array {("field", "comparator", "value"), ("field", "comparator", "value"), ...} 
	* @param string $sortBy 
	* @param boolean $ascending 
	* @param int limit 
	* @return array $remunerationList
	*/
	function GetList($fcv_array = array(), $sortBy='', $ascending=true, $limit='')
	{
		$connection = Database::Connect();
		$sqlLimit = ($limit != '' ? "LIMIT $limit" : '');
		$this->pog_query = "select * from `remuneration` ";
		$remunerationList = Array();
		if (sizeof($fcv_array) > 0)
		{
			$this->pog_query .= " where ";
			for ($i=0, $c=sizeof($fcv_array); $i<$c; $i++)
			{
				if (sizeof($fcv_array[$i]) == 1)
				{
					$this->pog_query .= " ".$fcv_array[$i][0]." ";
					continue;
				}
				else
				{
					if ($i > 0 && sizeof($fcv_array[$i-1]) != 1)
					{
						$this->pog_query .= " AND ";
					}
					if (isset($this->pog_attribute_type[$fcv_array[$i][0]]['db_attributes']) && $this->pog_attribute_type[$fcv_array[$i][0]]['db_attributes'][0] != 'NUMERIC' && $this->pog_attribute_type[$fcv_array[$i][0]]['db_attributes'][0] != 'SET')
					{
						if ($GLOBALS['configuration']['db_encoding'] == 1)
						{
							$value = POG_Base::IsColumn($fcv_array[$i][2]) ? "BASE64_DECODE(".$fcv_array[$i][2].")" : "'".$fcv_array[$i][2]."'";
							$this->pog_query .= "BASE64_DECODE(`".$fcv_array[$i][0]."`) ".$fcv_array[$i][1]." ".$value;
						}
						else
						{
							$value =  POG_Base::IsColumn($fcv_array[$i][2]) ? $fcv_array[$i][2] : "'".$this->Escape($fcv_array[$i][2])."'";
							$this->pog_query .= "`".$fcv_array[$i][0]."` ".$fcv_array[$i][1]." ".$value;
						}
					}
					else
					{
						$value = POG_Base::IsColumn($fcv_array[$i][2]) ? $fcv_array[$i][2] : "'".$fcv_array[$i][2]."'";
						$this->pog_query .= "`".$fcv_array[$i][0]."` ".$fcv_array[$i][1]." ".$value;
					}
				}
			}
		}
		if ($sortBy != '')
		{
			if (isset($this->pog_attribute_type[$sortBy]['db_attributes']) && $this->pog_attribute_type[$sortBy]['db_attributes'][0] != 'NUMERIC' && $this->pog_attribute_type[$sortBy]['db_attributes'][0] != 'SET')
			{
				if ($GLOBALS['configuration']['db_encoding'] == 1)
				{
					$sortBy = "BASE64_DECODE($sortBy) ";
				}
				else
				{
					$sortBy = "$sortBy ";
				}
			}
			else
			{
				$sortBy = "$sortBy ";
			}
		}
		else
		{
			$sortBy = "remunerationid";
		}
		$this->pog_query .= " order by ".$sortBy." ".($ascending ? "asc" : "desc")." $sqlLimit";
		$thisObjectName = get_class($this);
		$cursor = Database::Reader($this->pog_query, $connection);
		while ($row = Database::Read($cursor))
		{
			$remuneration = new $thisObjectName();
			$remuneration->remunerationId = $row['remunerationid'];
			$remuneration->type = $this->Unescape($row['type']);
			$remuneration->amount = $this->Unescape($row['amount']);
			$remunerationList[] = $remuneration;
		}
		return $remunerationList;
	}
	
	
	/**
	* Saves the object to the database
	* @return integer $remunerationId
	*/
	function Save($deep = true)
	{
		$connection = Database::Connect();
		$this->pog_query = "select `remunerationid` from `remuneration` where `remunerationid`='".$this->remunerationId."' LIMIT 1";
		$rows = Database::Query($this->pog_query, $connection);
		if ($rows > 0)
		{
			$this->pog_query = "update `remuneration` set 
			`type`='".$this->Escape($this->type)."', 
			`amount`='".$this->Escape($this->amount)."'where `remunerationid`='".$this->remunerationId."'";
		}
		else
		{
			$this->pog_query = "insert into `remuneration` (`type`, `amount`) values (
			'".$this->Escape($this->type)."', 
			'".$this->Escape($this->amount)."')";
		}
		$insertId = Database::InsertOrUpdate($this->pog_query, $connection);
		if ($this->remunerationId == "")
		{
			$this->remunerationId = $insertId;
		}
		if ($deep)
		{
			foreach ($this->_gotstudentactivityList as $gotstudentactivity)
			{
				$gotstudentactivity->remunerationId = $this->remunerationId;
				$gotstudentactivity->Save($deep);
			}
		}
		return $this->remunerationId;
	}
	
	
	/**
	* Clones the object and saves it to the database
	* @return integer $remunerationId
	*/
	function SaveNew($deep = false)
	{
		$this->remunerationId = '';
		return $this->Save($deep);
	}
	
	
	/**
	* Deletes the object from the database
	* @return boolean
	*/
	function Delete($deep = false, $across = false)
	{
		if ($deep)
		{
			$gotstudentactivityList = $this->GetGotstudentactivityList();
			foreach ($gotstudentactivityList as $gotstudentactivity)
			{
				$gotstudentactivity->Delete($deep, $across);
			}
		}
		$connection = Database::Connect();
		$this->pog_query = "delete from `remuneration` where `remunerationid`='".$this->remunerationId."'";
		return Database::NonQuery($this->pog_query, $connection);
	}
	
	
	/**
	* Deletes a list of objects that match given conditions
	* @param multidimensional array {("field", "comparator", "value"), ("field", "comparator", "value"), ...} 
	* @param bool $deep 
	* @return 
	*/
	function DeleteList($fcv_array, $deep = false, $across = false)
	{
		if (sizeof($fcv_array) > 0)
		{
			if ($deep || $across)
			{
				$objectList = $this->GetList($fcv_array);
				foreach ($objectList as $object)
				{
					$object->Delete($deep, $across);
				}
			}
			else
			{
				$connection = Database::Connect();
				$pog_query = "delete from `remuneration` where ";
				for ($i=0, $c=sizeof($fcv_array); $i<$c; $i++)
				{
					if (sizeof($fcv_array[$i]) == 1)
					{
						$pog_query .= " ".$fcv_array[$i][0]." ";
						continue;
					}
					else
					{
						if ($i > 0 && sizeof($fcv_array[$i-1]) !== 1)
						{
							$pog_query .= " AND ";
						}
						if (isset($this->pog_attribute_type[$fcv_array[$i][0]]['db_attributes']) && $this->pog_attribute_type[$fcv_array[$i][0]]['db_attributes'][0] != 'NUMERIC' && $this->pog_attribute_type[$fcv_array[$i][0]]['db_attributes'][0] != 'SET')
						{
							$pog_query .= "`".$fcv_array[$i][0]."` ".$fcv_array[$i][1]." '".$this->Escape($fcv_array[$i][2])."'";
						}
						else
						{
							$pog_query .= "`".$fcv_array[$i][0]."` ".$fcv_array[$i][1]." '".$fcv_array[$i][2]."'";
						}
					}
				}
				return Database::NonQuery($pog_query, $connection);
			}
		}
	}
	
	
	/**
	* Gets a list of GotStudentActivity objects associated to this one
	* @param multidimensional array {("field", "comparator", "value"), ("field", "comparator", "value"), ...} 
	* @param string $sortBy 
	* @param boolean $ascending 
	* @param int limit 
	* @return array of GotStudentActivity objects
	*/
	function GetGotstudentactivityList($fcv_array = array(), $sortBy='', $ascending=true, $limit='')
	{
		$gotstudentactivity = new GotStudentActivity();
		$fcv_array[] = array("remunerationId", "=", $this->remunerationId);
		$dbObjects = $gotstudentactivity->GetList($fcv_array, $sortBy, $ascending, $limit);
		return $dbObjects;
	}
	
	
	/**
	* Makes this the parent of all GotStudentActivity objects in the GotStudentActivity List array. Any existing GotStudentActivity will become orphan(s)
	* @return null
	*/
	function SetGotstudentactivityList(&$list)
	{
		$this->_gotstudentactivityList = array();
		$existingGotstudentactivityList = $this->GetGotstudentactivityList();
		foreach ($existingGotstudentactivityList as $gotstudentactivity)
		{
			$gotstudentactivity->remunerationId = '';
			$gotstudentactivity->Save(false);
		}
		$this->_gotstudentactivityList = $list;
	}
	
	
	/**
	* Associates the GotStudentActivity object to this one
	* @return 
	*/
	function AddGotstudentactivity(&$gotstudentactivity)
	{
		$gotstudentactivity->remunerationId = $this->remunerationId;
		$found = false;
		foreach($this->_gotstudentactivityList as $gotstudentactivity2)
		{
			if ($gotstudentactivity->gotstudentactivityId > 0 && $gotstudentactivity->gotstudentactivityId == $gotstudentactivity2->gotstudentactivityId)
			{
				$found = true;
				break;
			}
		}
		if (!$found)
		{
			$this->_gotstudentactivityList[] = $gotstudentactivity;
		}
	}
}
?>